package com.mz.ozy.mvp.presenter;

import android.app.Activity;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.FirmOrderBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.bean.ShoppingCartListBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.view.CarContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.RxUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;

/**
 * Created by Administrator on 2018/12/6.
 * 购物车
 */

public class CarPresenter extends BasePresenterImpl<CarContract.View> implements CarContract.Presenter {


    @Override
    public void initData(Activity activity, String pageNo, boolean load) {

//        if (!Constants.isLogin) {
//            mView.onInitError("未登录");
//            return;
//        }

        Map<String, String> maps = new HashMap<>();
        maps.put("pageNo", "0");
        maps.put("pageSize", Constants.PageSize);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10118, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<ShoppingCartListBean>(mView, load) {
                    @Override
                    public void onSuccesss(ShoppingCartListBean shoppingCartListBean) {
                        mView.onInitSuccess(shoppingCartListBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onInitError("");
                    }
                });
    }

    @Override
    public void operateGoods(Activity activity, String goodsList, String type) {
//        transCode		必须	DeleteShoppingCartV1
//        t		必须	Token
//        cartId		必须	购物车ID
        Map<String, String> maps = new HashMap<>();
        maps.put("carId", goodsList);
        maps.put("type", type);
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10119, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<NetBean>(mView, false) {
                    @Override
                    public void onSuccesss(NetBean netBean) {
                        if ("0".equals(type)) {
                            mView.onDeleteGoodsSuccess();
                        } else if ("1".equals(type)) {
                            mView.onEditGoodsCountSuccess("1");
                        } else if ("2".equals(type)) {
                            mView.onEditGoodsCountSuccess("2");
                        }
                    }
                });
    }

    @Override
    public void editGoodsCount(Activity activity, String goodsId, String count) {
//        transCode		必须	ShoppingCartUpdateV1
//        t		必须	token
//        cartId		必须	购物车ID
//        num		必须	数量
//        isCheck		必须	是否选中:1-选中 2-未选中

        Map<String, String> maps = new HashMap<>();
//        maps.put("transCode", Constants.TransCode.ShoppingCartUpdateV1);
//        maps.put("t", SharePreferencesData.getSharePreferencesData(SBMallApp.ctx, Constants.PREE.TOKEN_ID));
//        maps.put("cartId", goodsId);
//        maps.put("num", count);
//        maps.put("isCheck", String.valueOf(1));
//        RetrofitUtils.getApiService(activity).post(maps)
//                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
//                .subscribe(new MyObserver<NetBean>(mView, false) {
//                    @Override
//                    public void onSuccesss(NetBean netBean) {
//                        mView.onEditGoodsCountSuccess();
//                    }
//                });
    }

    @Override
    public void submit(Activity activity, String list, String type, String gid, int totalNum) {
//        transCode		必须	ShoppingCartConfirmOrderV1
//        t		必须	token
//        cid		必须	购物车ID 多个以,号隔开
        Map<String, String> maps = new HashMap<>();
        maps.put("list", list);
        maps.put("type", type);
        maps.put("gId", gid);
        maps.put("num", String.valueOf(totalNum));
        RetrofitUtils.getApiService(activity).post(Constants.TransCode.TransCode10120, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<FirmOrderBean>(mView, false) {

                    @Override
                    public void onSubscribe(Disposable d) {
                        super.onSubscribe(d);
                        mView.showLoading();
                    }

                    @Override
                    public void onSuccesss(FirmOrderBean firmOrderBean) {
                        mView.onSubmitSuccess(firmOrderBean, list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onSubmitError("");
                    }
                });
    }
}
